package org.abettor.angleworm.ui;

import java.io.IOException;

import org.abettor.angleworm.i18n.Translator;
import org.abettor.angleworm.ui.widget.ContactComposite;
import org.abettor.angleworm.ui.widget.HistoryComposite;
import org.abettor.angleworm.ui.widget.TeamComposite;
import org.abettor.angleworm.util.Configuration;
import org.abettor.util.Environment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;

/**
 * angleworm主要操作界面
 * @author shawn
 *
 */
public class MainDialog
{
	private static Translator translator = null;
	private static final int WIDTH = 304;
	private static final int HEIGHT = 700;
	private String account;

	private Display display = Display.getDefault();
	private Shell mainShell = null;
	private Label labelAvatar = null;
	private Label labelNick = null;
	private Combo comboStatus = null;
	private Combo comboMood = null;
	private TabFolder tabFolder = null;
	private TabItem tabItemContact = null;
	private TabItem tabItemTeam = null;
	private TabItem tabItemHistory = null;
	private ContactComposite contactComp = null;
	private TeamComposite teamComposite = null;
	private HistoryComposite historyComposite = null;
	private Composite functionComposite = null;
	private Button buttonAddContact = null;
	private Button buttonAddGroup = null;
	private Button buttonJoinTeam = null;
	private Button buttonSettings = null;

	public MainDialog(String account)
	{
		this.account = account;
	}

	/**
	 * 显示主窗口
	 */
	public void show()
	{
		createMainShell();
		mainShell.open();
		while(! mainShell.isDisposed())
		{
			if(! display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	/**
	 * 绘制主窗口
	 */
	private void createMainShell()
	{
		try
		{
			translator = Translator.getInstance();
			if(translator == null)
			{
				translator = Translator.initialize("en_US");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.numColumns = 3;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		mainShell = new Shell(SWT.SHELL_TRIM);
		mainShell.setText(translator.getString("main.dialog.title"));
		mainShell.setLayout(gridLayout);
		mainShell.setSize(new Point(WIDTH, HEIGHT));
		mainShell.setImage(new Image(display, Environment.getFilePath("img/logo.png")));
		Rectangle rect = Configuration.getMainDialogBounds();
		if(rect != null)
		{
			mainShell.setBounds(rect);
		}

		GridData gridDataAvatar = new GridData();
		gridDataAvatar.verticalAlignment = GridData.CENTER;
		gridDataAvatar.heightHint = 48;
		gridDataAvatar.widthHint = 48;
		gridDataAvatar.verticalSpan = 2;
		labelAvatar = new Label(mainShell, SWT.NONE | SWT.SHADOW_NONE);
		//TODO	设置头像图片
		labelAvatar.setImage(new Image(display, Environment.getFilePath("img/avatar/none.big.png")));
		labelAvatar.setLayoutData(gridDataAvatar);

		GridData gridDataNick = new GridData();
		gridDataNick.grabExcessHorizontalSpace = true;
		gridDataNick.horizontalIndent = 2;
		gridDataNick.widthHint = 150;
		gridDataNick.horizontalAlignment = GridData.FILL;
		labelNick = new Label(mainShell, SWT.NONE | SWT.SHADOW_NONE);
		labelNick.setText("User nick and other info here");
		labelNick.setLayoutData(gridDataNick);

		createComboStatus();
		createComboMood();
		createTabFolder();
		createFunctionComposite();

		mainShell.addShellListener(new ShellAdapter()
		{
			public void shellClosed(ShellEvent e)
			{
				//	确认是否要退出程序
				MessageBox confirm = new MessageBox(mainShell, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
				confirm.setText(translator.getString("main.dialog.quit.confirm.title"));
				confirm.setMessage(translator.getString("main.dialog.quit.confirm.message"));
				int val = confirm.open();
				if(val != SWT.YES)
				{
					e.doit = false;
					return;
				}

				//	保存配置信息
				Configuration.setMainDialogBounds(mainShell.getBounds());
				Configuration.save();

				//TODO	退出登录
			}
		});
	}

	private void createComboStatus()
	{
		GridData gridDataStatus = new GridData();
		comboStatus = new Combo(mainShell, SWT.NONE | SWT.READ_ONLY);
		comboStatus.setToolTipText(translator.getString("login.status.tooltip"));
		comboStatus.setLayoutData(gridDataStatus);
		String[] items = {translator.getString("login.status.online"),
				translator.getString("login.status.busy"),
				translator.getString("login.status.offline")};
		comboStatus.setItems(items);
		//TODO	设定当前状态
	}

	private void createComboMood()
	{
		GridData gridDataMood = new GridData();
		gridDataMood.horizontalAlignment = GridData.FILL;
		gridDataMood.grabExcessHorizontalSpace = true;
		gridDataMood.horizontalIndent = 2;
		gridDataMood.horizontalSpan = 2;
		comboMood = new Combo(mainShell, SWT.NONE);
		comboMood.setLayoutData(gridDataMood);
		String[] moods = Configuration.getUsedMoods(account);
		if(moods == null || moods.length == 0)
		{
			moods = new String[]{translator.getString("main.dialog.default.mood")};
		}
		comboMood.setItems(moods);
		String mood = Configuration.getCurrentMood(account);
		if(mood == null || mood.isEmpty())
		{
			mood = translator.getString("main.dialog.default.mood");
		}
		comboMood.setText(mood);
		Configuration.setCurrentMood(account, mood);
		

		comboMood.addFocusListener(new FocusAdapter()
		{
			public void focusLost(FocusEvent e)
			{
				String val = comboMood.getText();
				if(val == null || val.isEmpty())
				{
					val = Configuration.getCurrentMood(account);
					comboMood.setText(val);
					return;
				}

				Configuration.setCurrentMood(account, val);
				comboMood.setItems(Configuration.getUsedMoods(account));
				comboMood.setText(val);
				//TODO	将当前心情短语同步到网络
			}
		});
	}

	private void createTabFolder()
	{
		GridData gridDataTab = new GridData();
		gridDataTab.grabExcessHorizontalSpace = true;
		gridDataTab.grabExcessVerticalSpace = true;
		gridDataTab.horizontalAlignment = GridData.FILL;
		gridDataTab.verticalAlignment = GridData.FILL;
		gridDataTab.widthHint = 300;
		gridDataTab.heightHint = 300;
		gridDataTab.horizontalSpan = 3;
		tabFolder = new TabFolder(mainShell, SWT.NONE);
		tabFolder.setLayoutData(gridDataTab);

		createTabItemContact();
		createTabItemGroup();
		createTabItemHistory();
	}

	private void createTabItemContact()
	{
		tabItemContact = new TabItem(tabFolder, SWT.NONE);
		tabItemContact.setText(translator.getString("main.dialog.tab.contact"));
		contactComp = new ContactComposite(tabFolder, SWT.NONE);
		tabItemContact.setControl(contactComp);
	}

	private void createTabItemGroup()
	{
		tabItemTeam = new TabItem(tabFolder, SWT.NONE);
		tabItemTeam.setText(translator.getString("main.dialog.tab.group"));
		teamComposite = new TeamComposite(tabFolder, SWT.NONE);
		tabItemTeam.setControl(teamComposite);
	}

	private void createTabItemHistory()
	{
		tabItemHistory = new TabItem(tabFolder, SWT.NONE);
		tabItemHistory.setText(translator.getString("main.dialog.tab.history"));
		historyComposite = new HistoryComposite(tabFolder, SWT.NONE);
		tabItemHistory.setControl(historyComposite);
	}

	private void createFunctionComposite()
	{
		GridLayout gridLayoutCmp = new GridLayout();
		gridLayoutCmp.horizontalSpacing = 0;
		gridLayoutCmp.verticalSpacing = 0;
		gridLayoutCmp.marginWidth = 10;
		gridLayoutCmp.numColumns = 4;
		GridData gridDataCmp = new GridData();
		gridDataCmp.grabExcessHorizontalSpace = true;
		gridDataCmp.horizontalSpan = 3;
		gridDataCmp.horizontalAlignment = GridData.FILL;
		functionComposite = new Composite(mainShell, SWT.NONE);
		functionComposite.setLayoutData(gridDataCmp);
		functionComposite.setLayout(gridLayoutCmp);

		buttonAddContact = new Button(functionComposite, SWT.NONE);
		buttonAddContact.setLayoutData(new GridData());
		buttonAddContact.setImage(new Image(display, Environment.getFilePath("img/contact.png")));
		buttonAddContact.setToolTipText(translator.getString("main.dialog.function.add.contact.tooltip"));

		GridData gridDataAddGroup = new GridData();
		gridDataAddGroup.horizontalIndent = 5;
		buttonAddGroup = new Button(functionComposite, SWT.NONE);
		buttonAddGroup.setLayoutData(gridDataAddGroup);
		buttonAddGroup.setImage(new Image(display, Environment.getFilePath("img/group.png")));
		buttonAddGroup.setToolTipText(translator.getString("main.dialog.function.add.group.tooltip"));

		GridData gridDataJoinTeam = new GridData();
		gridDataJoinTeam.horizontalIndent = 5;
		buttonJoinTeam = new Button(functionComposite, SWT.NONE);
		buttonJoinTeam.setLayoutData(gridDataJoinTeam);
		buttonJoinTeam.setImage(new Image(display, Environment.getFilePath("img/team.png")));
		buttonJoinTeam.setToolTipText(translator.getString("main.dialog.function.join.team.tooltip"));

		GridData gridDataSettings = new GridData();
		gridDataSettings.grabExcessHorizontalSpace = true;
		gridDataSettings.horizontalAlignment = GridData.END;
		buttonSettings = new Button(functionComposite, SWT.NONE);
		buttonSettings.setLayoutData(gridDataSettings);
		buttonSettings.setImage(new Image(display, Environment.getFilePath("img/settings.png")));
		buttonSettings.setToolTipText(translator.getString("main.dialog.function.settings.tooltip"));
	}
}
